﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PersonaLibrary.Data;
using PersonaLibrary.Validation;

namespace PersonaLibrary.ViewModel.EmployeeNoteLinkViewModel
{
    public class EmployeeNoteLinkVM : LinqEditableItem<EmployeeNoteLink, View.NoteEmployeesLink.NoteEmployeeView>
    {
        private readonly ValidationItem<Employee> _empValid = new ValidationItem<Employee>();
        private readonly ValidationItem<Note> _notValid = new ValidationItem<Note>();

        public EmployeeNoteLinkVM()
        {
            EditableItem = new EmployeeNoteLink();
            InitValid();
        }

        public EmployeeNoteLinkVM(int id)
        {
            EditableItem = Context.EmployeeNoteLinks.Single(x => ((EmployeeNoteLink)x).Id == id);
            InitValid();
            _empValid.ValidationValue = EditableItem.Employee.ToString();
            _empValid.IsValid = true;
            _notValid.ValidationValue = EditableItem.Note.Name;
            _notValid.IsValid = true;
        }

        private void InitValid()
        {
            _empValid.Source = Context.Employees;
            _empValid.ValidExpression = x => x.Name.StartsWith(_empValid.ValidationValue);
            _empValid.Selector = x => x.ToString();
            _empValid.PropertyChanged += EmpValidPropertyChanged;
            _empValid.EntitySelector = x => new ValidEntity { SelectedKey = x.ToString(), SelectedValue = x.Id };

            _notValid.Source = Context.Notes;
            _notValid.ValidExpression = x => x.Name.ToUpper().StartsWith(_notValid.ValidationValue.ToUpper());
            _notValid.Selector = x => x.Name;
        }

        void EmpValidPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "ValidationValue")
            {
                System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(@"\s+");
                string[] result = reg.Replace(_empValid.ValidationValue, " ").Split(" ".ToCharArray());
                switch (result.Length)
                {
                    case 0:
                        break;
                    case 1:
                        _empValid.ValidExpression = x => x.Surname.StartsWith(result[0]);
                        break;
                    case 2:
                        _empValid.ValidExpression = x => x.Surname.StartsWith(result[0]) && x.Name.StartsWith(result[1]);
                        break;
                    case 3:
                        _empValid.ValidExpression = x => x.Surname.StartsWith(result[0]) && x.Name.StartsWith(result[1]) && x.Patronymic.StartsWith(result[2]);
                        break;
                }
            }
        }

        public ValidationItem<Employee> EmpValid
        {
            get
            {
                return _empValid;
            }
        }

        public ValidationItem<Note> NotValid
        {
            get
            {
                return _notValid;
            }
        }

        public override bool CanCommit()
        {
            if (!_notValid.IsValid) return false;
            if (!_empValid.IsValid) return false;
            return true;
        }

        public override void Commit()
        {
            int empployeeId = Convert.ToInt32(_empValid.Value.SelectedValue);
            EditableItem.EmployeeId = empployeeId;
            EditableItem.Note = Context.Notes.Single(x => x.Name == _notValid.ValidationValue);

            base.Commit();
        }
    }
}
